package com.hd.until;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DateFormatUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(DateFormatUtil.class);

    public static final String FORMAT_STRING_LONG = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_STRING_MIDDLE = "yyyy-MM-dd";
    public static final String FORMAT_STRING_SHORT = "MM-dd";
    public static final String FORMAT_STRING_HOURS = "HH:mm:ss";

    public static String formatDate(Date date, String format) {
        String sDate;
		if (date == null) {
		    sDate = null;
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            sDate = simpleDateFormat.format(date);
        }
		return sDate;
	}

	
    public static String longFormatDate(Date date) {
		return formatDate(date, FORMAT_STRING_LONG);
	}

	public static String middleFormatDate(Date date) {
		return formatDate(date, FORMAT_STRING_MIDDLE);
	}

	public static String hoursFormatDate(Date date) {
		return formatDate(date, FORMAT_STRING_HOURS);
	}
	
	public static String shortFormatDate(Date date) {
		return formatDate(date, FORMAT_STRING_SHORT);
	}

    /**
     * 两个日期之间间隔的小时
     * @param date1
     * @param date2
     * @return
     */
    public static Integer dateBetweenHours(Date date1, Date date2){
        Long millSecs = date2.getTime() - date1.getTime();
        Long hours = millSecs / (1000 * 60 * 60);
        return hours.intValue();
    }
    
    /**
     * 两个日期之间间隔的天数
     * @param date1
     * @param date2
     * @return
     */
    public static Integer dateBetweenDays(Date date1, Date date2){
        Long millSecs = date2.getTime() - date1.getTime();
        if(millSecs <= 0){
            return 0;
        }
        int timeUnit = 1000 * 60 * 60 * 24;
        Long day = 0l;
        if(millSecs % timeUnit == 0){
            day = millSecs / timeUnit;
        }else{
            day = millSecs / timeUnit + 1;
        }
        return day.intValue();
    }
    
    //在某日期基础上加x个月
    public static String addMonth(String date,Integer month) {  
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
    	Calendar cd = Calendar.getInstance();  
    	try {
			cd.setTime(sdf.parse(date));
			cd.add(Calendar.MONTH, month);//增加x个月
		} catch (ParseException e) {
			LOGGER.error("addMonth Parse Date String Error", e);
		} 
  
    	return sdf.format(cd.getTime());
    }
    
    //在某日期基础上加x天
    public static String addDay(String date,Integer day) {  
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
    	Calendar cd = Calendar.getInstance();  
    	try {
			cd.setTime(sdf.parse(date));
			cd.add(Calendar.DATE, day);//增加x天
		} catch (ParseException e) {
			LOGGER.error("addMonth Parse Date String Error", e);
		} 
  
    	return sdf.format(cd.getTime());
    } 
    
    /**
     * 日期相加
     * @param date
     * @param days
     * @return
     */
    public static Date add(Date date,int days){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH,days);
        return calendar.getTime();
    }
    
    
    /**
     * 日期相加
     * @param date
     * @param days
     * @return
     */
    public static int getDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }
    
}
